home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Super Collection / Windows 95 Super Collection.iso / win95 / bench / thread / thrdtsts.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-13  |  3.6 KB  |  155 lines

  1. //==========================================================================
  2. // THRDTSTS.CPP - routines that run thread tests (primes and graphics).
  3. //==========================================================================
  4. #include <windows.h>
  5. #include <stdlib.h>
  6.  
  7. #include "thrdtsts.h"
  8.  
  9. UINT DoPrimesCalc(UINT uTotalPrimes, UINT uNumPrimesThisPass, LONG *pPrimesArray, UINT uNumCurrentPrimes)
  10. {
  11.     long lTestVal = 1L;
  12.  
  13.     long lModulus;
  14.  
  15.     BOOL bFactorFound = FALSE;
  16.  
  17.     UINT uFoundPrimes = uNumCurrentPrimes;
  18.  
  19.     UINT uTotalPrimesThisPass = 0;
  20.     
  21.     if(uFoundPrimes == 0)
  22.     {
  23.         // Giving ourselves a head start with 2 as first prime
  24.         pPrimesArray[0] = 2;
  25.         uFoundPrimes = 1;
  26.         uTotalPrimesThisPass++;
  27.     }
  28.         
  29.     // Calculate. primes using array as factors.
  30.     while(1)
  31.     {
  32.         lTestVal += 2; // Skip even numbers.            
  33.  
  34.         bFactorFound = FALSE;
  35.  
  36.         for(UINT i = 0; i < uFoundPrimes; i++) 
  37.         {
  38.             lModulus = lTestVal % pPrimesArray[i];
  39.             if(!lModulus)
  40.             {        
  41.                 bFactorFound = TRUE;                
  42.                 break;
  43.             }
  44.         }
  45.     
  46.         if(!bFactorFound)
  47.         {
  48.             // Then it's a prime.
  49.             pPrimesArray[uFoundPrimes] = lTestVal;
  50.             uFoundPrimes++;
  51.             uTotalPrimesThisPass++;
  52.             if(uFoundPrimes >= uTotalPrimes || uTotalPrimesThisPass >= uNumPrimesThisPass)
  53.                 break;
  54.         }
  55.     }
  56.  
  57.     // Return total in whole array so we know when to stop.
  58.     return uFoundPrimes;
  59. }
  60.  
  61. UINT DoGraphics(HWND hwndDraw, UINT uTotalReps, UINT uRepsThisPass, UINT uCurrentReps)
  62. {
  63.     // Draws random shapes in specified window.
  64.     // Uses specified number of repetitions unless
  65.     // 0--then continue indefinitely.
  66.     RECT rClient;
  67.     
  68.     int nLines;
  69.  
  70.     HPEN hpenOld;
  71.     HBRUSH hbrOld;
  72.  
  73.     HPEN hpenNew;
  74.     HPEN hbrNew;
  75.  
  76.     HDC hdc = GetDC(hwndDraw);
  77.     
  78.     GetClientRect(hwndDraw, &rClient);
  79.     
  80.     for(UINT i = 0; i < uRepsThisPass; i++)
  81.     {
  82.          // Draw random rectangle, random ellipse and random line.
  83.        hpenNew = CreatePen(PS_SOLID, 2, RGB(rand() % 256, rand() % 256, rand() % 256));  
  84.        hbrNew = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256));    
  85.                  
  86.         hpenOld = SelectObject(hdc, hpenNew);
  87.         hbrOld = SelectObject(hdc, hbrNew);
  88.  
  89.         Rectangle(hdc, 
  90.                      rand() % rClient.right,
  91.                      rand() % rClient.bottom,
  92.                      rand() % rClient.right,
  93.                      rand() % rClient.bottom);
  94.                      
  95.         SelectObject(hdc, hpenOld);
  96.         SelectObject(hdc, hbrOld);
  97.  
  98.         DeleteObject(hpenNew);
  99.         DeleteObject(hbrNew);
  100.  
  101.         // New colors for ellipse.
  102.         hpenNew = CreatePen(PS_SOLID, 2, RGB(rand() % 256, rand() % 256, rand() % 256));  
  103.        hbrNew = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256));    
  104.                  
  105.         hpenOld = SelectObject(hdc, hpenNew);
  106.         hbrOld = SelectObject(hdc, hbrNew);
  107.  
  108.         Ellipse(hdc, 
  109.                      rand() % rClient.right,
  110.                      rand() % rClient.bottom,
  111.                      rand() % rClient.right,
  112.                      rand() % rClient.bottom);
  113.                      
  114.         SelectObject(hdc, hpenOld);
  115.         SelectObject(hdc, hbrOld);
  116.  
  117.         DeleteObject(hpenNew);
  118.         DeleteObject(hbrNew);
  119.         
  120.         MoveToEx(hdc, 
  121.                  rand() % rClient.right,
  122.                  rand() % rClient.bottom,
  123.                  NULL);
  124.               
  125.         // New colors for line.
  126.         nLines = rand() % 10;
  127.         for(int j = 0; j < nLines; j++)
  128.         {        
  129.             hpenNew = CreatePen(PS_SOLID, 2, RGB(rand() % 256, rand() % 256, rand() % 256));  
  130.                  
  131.             hpenOld = SelectObject(hdc, hpenNew);
  132.  
  133.             LineTo(hdc, 
  134.                  rand() % rClient.right,
  135.                  rand() % rClient.bottom);
  136.                      
  137.             SelectObject(hdc, hpenOld);
  138.         
  139.             DeleteObject(hpenNew);
  140.         }
  141.  
  142.         if(uTotalReps)
  143.         {
  144.             if((i + uCurrentReps) >= uTotalReps)
  145.                 break;  // Max. reps completed; we're done.
  146.         }
  147.     }
  148.     
  149.     ReleaseDC(hwndDraw, hdc);
  150.  
  151.     return i + uCurrentReps;
  152. }
  153.  
  154.  
  155.